Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke

Namespace OliverHine.DNN.Weather

    Public MustInherit Class Settings
		Inherits Entities.Modules.ModuleSettingsBase

#Region "Controls"
        Protected WithEvents litScripts As System.Web.UI.WebControls.Literal
        Protected WithEvents chkPerUser As System.Web.UI.WebControls.CheckBox
        Protected WithEvents chkAllowInput As System.Web.UI.WebControls.CheckBox
        Protected WithEvents chkSeparateForecast As System.Web.UI.WebControls.CheckBox
        Protected WithEvents rblSeparateOptions As System.Web.UI.WebControls.RadioButtonList
        Protected WithEvents chkDetailedToday As System.Web.UI.WebControls.CheckBox
        Protected WithEvents ddlUnit As System.Web.UI.WebControls.DropDownList
        Protected WithEvents ddlIconLarge As System.Web.UI.WebControls.DropDownList
        Protected WithEvents ddlIconSmall As System.Web.UI.WebControls.DropDownList
        Protected WithEvents ddlExtForecast As System.Web.UI.WebControls.DropDownList
        Protected WithEvents ddlLocation As System.Web.UI.WebControls.DropDownList
        Protected WithEvents tbxPartnerID As System.Web.UI.WebControls.TextBox
        Protected WithEvents rPartnerID As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents tbxLicenseKey As System.Web.UI.WebControls.TextBox
        Protected WithEvents rLicenseKey As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents pnlWeatherDOTcom As System.Web.UI.WebControls.Panel
        Protected WithEvents ucSearch As OliverHine.Weather.Search
#End Region

        Public Overrides Sub LoadSettings()
            Try
                If Not Page.IsPostBack Then
                    ' Load settings from ModuleSettings: specific to this instance
                    litScripts.Text = String.Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveUrl("~/desktopmodules/weather/weather.js"))
                    If ModuleSettings.Contains("SeparateForecast") Then
                        Dim bSeparateForecast As Boolean = CType(ModuleSettings("SeparateForecast"), Boolean)
                        chkSeparateForecast.Checked = bSeparateForecast

                        If bSeparateForecast Then
                            rblSeparateOptions.Visible = True
                        Else
                            rblSeparateOptions.Visible = False
                        End If

                        If ModuleSettings.Contains("SeparateOptions") AndAlso Common.Utilities.Null.IsNull(ModuleSettings("SeparateOptions").ToString) = False Then
                            rblSeparateOptions.Items.FindByValue(ModuleSettings("SeparateOptions").ToString).Selected = True
                        Else
                            rblSeparateOptions.Items(0).Selected = True
                            rblSeparateOptions.Visible = False
                        End If
                    Else
                        rblSeparateOptions.Visible = False
                    End If

                    RegisterDHTMLItems(rblSeparateOptions)

                    If ModuleSettings.Contains("PerUser") Then
                        chkPerUser.Checked = CType(ModuleSettings("PerUser"), Boolean)
                    End If
                    If ModuleSettings.Contains("DetailedToday") Then
                        chkDetailedToday.Checked = CType(ModuleSettings("DetailedToday"), Boolean)
                    End If
                    If ModuleSettings.Contains("ZipCode") Then
                        ucSearch.WeatherLocation = ModuleSettings("ZipCode").ToString()
                    End If
                    If ModuleSettings.Contains("ExtForecastLength") Then
                        If Not ddlExtForecast.Items.FindByValue(ModuleSettings("ExtForecastLength").ToString()) Is Nothing Then
                            ddlExtForecast.Items.FindByValue(ModuleSettings("ExtForecastLength").ToString()).Selected = True
                        Else
                            ddlExtForecast.Items.FindByValue("5").Selected = True
                        End If
                    Else
                        ddlExtForecast.Items.FindByValue("5").Selected = True
                    End If
                    If ModuleSettings.Contains("Unit") Then
                        If Not ddlUnit.Items.FindByValue(ModuleSettings("Unit").ToString()) Is Nothing Then
                            ddlUnit.Items.FindByValue(ModuleSettings("Unit").ToString()).Selected = True
                        Else
                            ddlUnit.Items.FindByValue("f").Selected = True
                        End If
                    Else
                        ddlUnit.Items.FindByValue("f").Selected = True
                    End If
                    If ModuleSettings.Contains("IconLarge") Then
                        If Not ddlIconLarge.Items.FindByValue(ModuleSettings("IconLarge").ToString) Is Nothing Then
                            ddlIconLarge.Items.FindByValue(ModuleSettings("IconLarge").ToString).Selected = True
                        Else
                            ddlIconLarge.Items.FindByValue("64x64").Selected = True
                        End If
                    Else
                        ddlIconLarge.Items.FindByValue("64x64").Selected = True
                    End If
                    If ModuleSettings.Contains("IconSmall") Then
                        If Not ddlIconSmall.Items.FindByValue(ModuleSettings("IconSmall").ToString) Is Nothing Then
                            ddlIconSmall.Items.FindByValue(ModuleSettings("IconSmall").ToString).Selected = True
                        Else
                            ddlIconSmall.Items.FindByValue("64x64").Selected = True
                        End If
                    Else
                        ddlIconSmall.Items.FindByValue("64x64").Selected = True
                    End If

                    If ModuleSettings.Contains("DisplayLocation") Then
                        If Not ddlLocation.Items.FindByValue(ModuleSettings("DisplayLocation").ToString) Is Nothing Then
                            ddlLocation.Items.FindByValue(ModuleSettings("DisplayLocation").ToString).Selected = True
                        End If
                    End If
                    If ModuleSettings.Contains("UserInput") Then
                        chkAllowInput.Checked = Boolean.Parse(ModuleSettings("UserInput").ToString)
                    End If
                    If ModuleSettings.Contains("WeatherStation") Then
                        ucSearch.WeatherLocation = ModuleSettings("WeatherStation").ToString
                    End If

                    ' Load settings from ModuleSettings: general for all instances
                    If Not ConfigurationSettings.AppSettings.Item("Weather.PartnerId") Is Nothing And Not ConfigurationSettings.AppSettings.Item("Weather.LicenseKey") Is Nothing Then
                        tbxPartnerID.Enabled = False
                        tbxPartnerID.Text = ConfigurationSettings.AppSettings.Item("Weather.PartnerId").ToString()

                        tbxLicenseKey.Enabled = False
                        tbxLicenseKey.Text = ConfigurationSettings.AppSettings.Item("Weather.LicenseKey").ToString()
                    Else
                        If ModuleSettings.Contains("PartnerID") Then
                            tbxPartnerID.Text = ModuleSettings("PartnerID").ToString()
                        End If
                        If ModuleSettings.Contains("LicenseKey") Then
                            tbxLicenseKey.Text = ModuleSettings("LicenseKey").ToString()
                        End If
                    End If

                End If
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.LogException(exc)
            End Try
        End Sub

        Public Overrides Sub UpdateSettings()
            Try
                Dim objModules As New Entities.Modules.ModuleController

                ' Update ModuleSettings
                If ConfigurationSettings.AppSettings.Item("Weather.PartnerId") Is Nothing And ConfigurationSettings.AppSettings.Item("Weather.LicenseKey") Is Nothing Then

                    objModules.UpdateModuleSetting(ModuleId, "PartnerID", tbxPartnerID.Text)
                    objModules.UpdateModuleSetting(ModuleId, "LicenseKey", tbxLicenseKey.Text)

                End If


                ' Update ModuleSettings
                objModules.UpdateModuleSetting(ModuleId, "WeatherStation", ucSearch.WeatherLocation)
                objModules.UpdateModuleSetting(ModuleId, "ExtForecastLength", ddlExtForecast.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "SeparateForecast", chkSeparateForecast.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "PerUser", chkPerUser.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "DetailedToday", chkDetailedToday.Checked.ToString)
                objModules.UpdateModuleSetting(ModuleId, "Unit", ddlUnit.SelectedValue)

                If Common.Utilities.Null.IsNull(rblSeparateOptions.SelectedValue) = False Then
                    objModules.UpdateModuleSetting(ModuleId, "SeparateOptions", rblSeparateOptions.SelectedValue)
                End If

                objModules.UpdateModuleSetting(ModuleId, "IconLarge", ddlIconLarge.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "IconSmall", ddlIconSmall.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "DisplayLocation", ddlLocation.SelectedValue)
                objModules.UpdateModuleSetting(ModuleId, "UserInput", chkAllowInput.Checked.ToString)

                'CheckModuleCache()

                ' Redirect back to the portal home page
                Response.Redirect(Common.NavigateURL(TabId), False)
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.LogException(exc)
            End Try
        End Sub

        Private Sub chkSeparateForecast_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSeparateForecast.CheckedChanged
            rblSeparateOptions.Visible = chkSeparateForecast.Checked
        End Sub

        Private Sub RegisterDHTMLItems(ByRef rbl As RadioButtonList)
            If DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(DotNetNuke.UI.Utilities.ClientAPI.ClientFunctionality.DHTML) Then
                If rbl.Visible = False Then
                    rbl.Visible = True
                    rbl.Style.Add("display", "none")
                Else
                    rbl.Style.Remove("display")
                End If

                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientReference(Page, DotNetNuke.UI.Utilities.ClientAPI.ClientNamespaceReferences.dnn_dom)
                chkSeparateForecast.Attributes.Add("onclick", "WeatherCheckBoxMaxMin(this, '" & rbl.ClientID & "')")
            End If
        End Sub

        Private Sub BuildListCollections()
            Dim li As New ListItem

            li.Value = "d"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("SeparateOptions.Default")
            rblSeparateOptions.Items.Add(li)

            li = New ListItem
            li.Value = "b"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("SeparateOptions.Both")
            rblSeparateOptions.Items.Add(li)

            li = New ListItem
            li.Value = "e"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("SeparateOptions.Extended")
            rblSeparateOptions.Items.Add(li)

            li = New ListItem
            li.Value = "t"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("SeparateOptions.Today")
            rblSeparateOptions.Items.Add(li)


            li = New ListItem
            li.Value = "1"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.1")
            ddlExtForecast.Items.Add(li)

            li = New ListItem
            li.Value = "2"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.2")
            ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "3"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.3")
            ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "4"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.4")
            ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "5"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.5")
            ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "6"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.6")
            'ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "7"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.7")
            'ddlExtForecast.Items.Add(li)

            li = New ListItem
            li.Value = "8"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.8")
            'ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "9"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.9")
            'ddlExtForecast.Items.Add(li)


            li = New ListItem
            li.Value = "10"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("ExtendedForecast.10")
            'ddlExtForecast.Items.Add(li)

            li = New ListItem
            li.Value = "dnam"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("LocationTitle.Name")
            ddlLocation.Items.Add(li)

            li = New ListItem
            li.Value = "obst"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("LocationTitle.Observatory")
            ddlLocation.Items.Add(li)

            li = New ListItem
            li.Value = "none"
            li.Text = OliverHine.Controls.Helpers.Localization.GetString("LocationTitle.None")
            ddlLocation.Items.Add(li)



        End Sub

        Private Sub LocalizeControls()
            chkPerUser.Text = OliverHine.Controls.Helpers.Localization.GetString("PerUser.Text")
            chkAllowInput.Text = OliverHine.Controls.Helpers.Localization.GetString("AllowInput.Text")
            chkSeparateForecast.Text = OliverHine.Controls.Helpers.Localization.GetString("SeparateForecast.Text")
            chkDetailedToday.Text = OliverHine.Controls.Helpers.Localization.GetString("DetailedToday.Text")

        End Sub

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub


        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()

            LocalizeControls()

            BuildListCollections()
        End Sub

#End Region

    End Class

End Namespace
